package com.gdzn.netty.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.gdzn.netty.channel.ChannelTcpMap;
import com.gdzn.netty.enums.RedisNettyKeyEnum;
import com.gdzn.netty.service.IDeviceRedisService;
import com.gdzn.common.redis.service.RedisService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @description: 电柜/电池设备和通道存储关系
 * @author: zdj
 * @created: 2024-04-18
 * @version: V1.0
 */
@Slf4j
@Component
public class DeviceRedisServiceImpl implements IDeviceRedisService {

    /**
     * 默认过期时长1天，单位：秒
     */
    private final static long DEFAULT_EXPIRE = 60 * 60 * 24;

    @Autowired
    private RedisService redisService;


    /**
     * 通过设备ID获取管道
     *
     * @param devId
     * @return
     */
    @Override
    public ChannelHandlerContext getChannelByDevId(String devId) {
        String channelId = redisService.getCacheObject(RedisNettyKeyEnum.preScreenfixKey.getKey()+devId);
        if(null!=channelId && StringUtils.isNotBlank(channelId)){
            return getChannel(channelId);
        }else{
            return null;
        }
    }

    /**
     * 通过channelId获取管道的关系
     *
     * @param channelId
     * @return
     */
    @Override
    public ChannelHandlerContext getChannel(String channelId) {
        ChannelHandlerContext cxt = ChannelTcpMap.getChannelByName(channelId);
        //log.info("通过channelId:{}获取管道的关系:ChannelHandlerContext:{}", channelId, cxt);
        return cxt;
    }

    /**
     * 删除管道
     *
     * @param ctx
     * @return
     */
    @Override
    public void removeChannel(ChannelHandlerContext ctx) {
        String channelId = ctx.channel().id().asShortText();
        ctx.channel().close();
        ChannelTcpMap.removeChannelByName(channelId);
        //log.info("***删除及关闭管道channelId:{}", channelId);
    }

    /**
     * 通过设备ID设置通道ID
     * 有效时间一天
     * @param devId
     * @param channelId
     * @return
     */
    @Override
    public boolean setChannelIdByDevId(String devId, String channelId) {
        redisService.setCacheObject(RedisNettyKeyEnum.preScreenfixKey.getKey()+devId, channelId);
        return true;
    }

    /**
     * 通过设备ID获取管道id
     *
     * @param devId
     * @return
     */
    @Override
    public String getChannelIdByDevId(String devId) {
        return redisService.getCacheObject(RedisNettyKeyEnum.preScreenfixKey.getKey()+devId);
    }

    /**
     * 通过设备删除管道id
     * @return
     */
    @Override
    public void removeChannelIdByDevId(String devId){
        redisService.deleteObject(RedisNettyKeyEnum.preScreenfixKey.getKey()+devId);
    }

    /**
     * 通过管道ID设置设备ID
     * @return
     */
    @Override
    public boolean setDevIdByChannelId(String channelId, String devId) {
        redisService.setCacheObject(RedisNettyKeyEnum.preChannelIdfixKey.getKey()+channelId, devId);
        return true;
    }

    /**
     * 通过管道ID获取设备ID
     * @return
     */
    @Override
    public String getDevIdByChannelId(String channelId) {
        return redisService.getCacheObject(RedisNettyKeyEnum.preChannelIdfixKey.getKey()+channelId);
    }

    /**
     * 通过管道ID删除管道id
     * @return
     */
    @Override
    public void removeDevIdByChannelId(String channelId) {
        redisService.deleteObject(RedisNettyKeyEnum.preChannelIdfixKey.getKey()+channelId);
    }

    /**
     * 鉴权
     * 从redis缓存获取设备ID，如果能获取到说明合法设备
     * @param devId
     * @return
     */
    @Override
    public String getDevId(String devId) {
        return redisService.getCacheObject("device:cabinet:devid:"+devId);
    }

    /**
     * 从redis缓存获取电柜鉴权是否需要
     * @return
     */
    @Override
    public String getAuthCabinet() {
        return redisService.getCacheObject("sys_config:cabinet.auth");
    }
}
